package pixelwaks.plugins.modes;

import java.awt.Color;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

import common.core.Tile;
import common.gui.IViewable;
import common.util.Calculate;

import pixelwaks.core.impl.Game;
import pixelwaks.core.impl.Player;
import pixelwaks.core.impl.Weapon;
import pixelwaks.ctrl.ControlFactory;
import pixelwaks.ctrl.impl.PlayerCtrl;
import pixelwaks.plugins.GameMode;

public class ShootYellowTilesMode extends GameMode {

	List<IViewable> observers = new ArrayList<IViewable>();
	
	@Override
	public boolean projectileHitSomething(Point p) {
				
		return hitScenery(p.x / 10, p.y / 10);
	}

	@Override
	public boolean isGameOver() {
		
		List<Weapon> weapons = Game.getInstance().getCurrentPlayer().getWeapons();		
		int counter = 0;
		
		for(Weapon w: weapons) {
			counter += w.getNumberOfShots();
		}
		
		boolean a = counter == 0;				
		Tile[][] tiles = Game.getInstance().getScenery().getTiles();
		counter = 0;
		
		for(int y = 0; y < tiles.length; y++)
			for(int x = 0; x < tiles[y].length; x++) {
				if(tiles[y][x] != null) {
					Color color = tiles[y][x].getColor();
					if(color.getRed() == 255 && color.getGreen() == 255 && color.getBlue() == 0 && tiles[y][x].isDestructible())
						counter++;
				}
			}
		
		boolean b = counter == 0;
		
		if(a || b)
			setGameOverInfo(whoWonTheGame().getName() + " won the game! Score: " + (int) whoWonTheGame().getStatus() + "p");
		
		return (a || b);
	}

	@Override
	public void hitByExplosion(Point p, int strength) {
		
		checkIfSceneryGotHitByExplosion(p, strength);        
	}
	
	private Player whoWonTheGame() {
		
		Player player = null;
		double points = 0.0;
		for(Player p: Game.getInstance().getPlayers())
			if(points < p.getStatus()) {
				points = p.getStatus();
				player = p;
			}
		
		return player;
	}
	
	private boolean hitScenery(int x, int y) {
		
		if(x < 100 && y < 60 && x > 0 && y > 0)
			if(Game.getInstance().getScenery().getTiles()[y][x] != null) {
				return true;
			}
		return false;
	}
	
	private void checkIfSceneryGotHitByExplosion(Point p, int strength) {

		int startXpos = (p.x - (strength * Tile.TILE_SIZE)) / Tile.TILE_SIZE;
		int startYpos = (p.y - (strength * Tile.TILE_SIZE)) / Tile.TILE_SIZE;
		int slutXpos  = (p.x + (strength * Tile.TILE_SIZE)) / Tile.TILE_SIZE;
		int slutYpos  = (p.y + (strength * Tile.TILE_SIZE)) / Tile.TILE_SIZE;	
		int radius = strength * Tile.TILE_SIZE + (Tile.TILE_SIZE / 2);
		Tile[][] tiles = Game.getInstance().getScenery().getTiles();

		for(int i = startYpos; i <= slutYpos; i++) {
			for(int j = startXpos; j <= slutXpos; j++) {
				if(i >= 0 && i < tiles.length && j >= 0 && j < tiles[i].length) {												
					if(tiles[i][j] != null && tiles[i][j].isDestructible() && Calculate.isInBlastArea(p.x, p.y, j*Tile.TILE_SIZE+5, i*Tile.TILE_SIZE+5, radius)) {
						Color color = tiles[i][j].getColor();
						if(color.getRed() == 255 && color.getGreen() == 255 && color.getBlue() == 0) {
							PlayerCtrl pctrl = (PlayerCtrl) ControlFactory.getObserver(ControlFactory.PLAYER_CTRL);
							double status = Game.getInstance().getCurrentPlayer().getStatus();
							pctrl.setStatus(Game.getInstance().getCurrentPlayer(), ++status);
						}
						tiles[i][j] = null;
					}
				}
			}
		}		
		
		for(IViewable iv: observers)
			iv.updateView(null);
	}

	@Override
	public void otherHitByExplosion(Point p, int strength) {
		// TODO Auto-generated method stub
		
	}
}